﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects;
using System.Web;
using Common;

namespace DAL
{
    public class Sys_Para
    {
        //-----------NewsType Begin--------------------//
        public List<NewsType>  SelectNewsType()
        {
            using (CIMEntities en = new CIMEntities())
            {
                var query = from n in en.NewsTypeSet
                            orderby n.TypeKey ascending
                            select n;
                return query.ToList();                           
            }
        }

        public bool AddNewsType(NewsType newstype)
        {
            using (CIMEntities en = new CIMEntities())
            {
                try
                {
                    en.AddObject("NewsTypeSet", newstype);
                    en.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public bool EditNewsType(NewsType newstype)
        {
            using (CIMEntities en = new CIMEntities())
            {
                try
                {
                    NewsType NewsType = en.NewsTypeSet.FirstOrDefault(n => n.TypeKey == newstype.TypeKey);
                    NewsType.TypeName = newstype.TypeName;
                    en.SaveChanges();
                    return true;
                }
                catch(Exception ex)
                {
                    return false;
                }
            }
        }

        public void DelNewsType(int TypeKey)
        {
            CIMDataContext db = new CIMDataContext();
            int count = db.Dat_News.Count(n => n.ClsID.Contains(TypeKey.ToString()));

            if (count > 0)
            {
                WebUtility.JsAlertToBack("已在使用中,不能删除");
                HttpContext.Current.Response.End();
            }
            else
            {
                using (CIMEntities en = new CIMEntities())
                {
                    var NewsType = en.NewsTypeSet.FirstOrDefault(n => n.TypeKey == TypeKey);
                    en.DeleteObject(NewsType);
                    en.SaveChanges();
                }
            }
        }

        //-----------NewsType End--------------------//

        //-----------AccountType Begin--------------------//

        public List<AccountType> SelectAccountType()
        {
            using (CIMEntities en = new CIMEntities())
            {   
                string esql = " select value n from AccountTypeSet as n ";
                ObjectQuery<AccountType> query = en.CreateQuery<AccountType>(esql);
                return query.Execute(MergeOption.NoTracking).ToList();
            }
        }

        public bool AddAccountType(AccountType accounttype)
        {
            using (CIMEntities en = new CIMEntities())
            {
                try
                {
                    en.AddObject("AccountTypeSet", accounttype);
                    en.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public bool EditAccountType(AccountType accounttype)
        {
            using (CIMEntities en = new CIMEntities())
            {
                try
                {
                    AccountType AccountType = en.AccountTypeSet.Where("it.TypeKey=@TypeKey", new ObjectParameter("TypeKey", accounttype.TypeKey)).FirstOrDefault();
                    AccountType.TypeName = accounttype.TypeName;
                    en.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public void DelAccountType(int TypeKey)
        {
            CIMDataContext db = new CIMDataContext();
            int count = db.Sys_Account.Count(a => a.State == 0 && a.UserType == TypeKey);

            if (count > 0)
            {
                WebUtility.JsAlertToBack("已在使用中,不能删除");
                HttpContext.Current.Response.End();
            }
            else
            {   
                using (CIMEntities en = new CIMEntities())
                {
                    var AccountType = en.AccountTypeSet.FirstOrDefault(a => a.TypeKey == TypeKey);
                    en.DeleteObject(AccountType);
                    en.SaveChanges();
                }
            }
        }

        //-----------AccountType End--------------------//

        //-----------Sys_MonthDan Begin--------------------//

        public Sys_MonthDan SelectMonthDan(int DanKey)
        {
            using (CIMEntities en = new CIMEntities())
            {
                string esql = " select value n from MonthDanSet as n where n.DanKey=@DanKey";
                ObjectQuery<Sys_MonthDan> query = en.CreateQuery<Sys_MonthDan>(esql, new ObjectParameter("DanKey", DanKey));
                return query.Execute(MergeOption.NoTracking).FirstOrDefault();
            }
        }

        public List<Sys_MonthDan> SelectMonthDan()
        {
            using (CIMEntities en = new CIMEntities())
            {
                string esql = " select value n from MonthDanSet as n ";
                ObjectQuery<Sys_MonthDan> query = en.CreateQuery<Sys_MonthDan>(esql);
                return query.Execute(MergeOption.NoTracking).ToList();
            }
        }

        public bool AddMonthDan(Sys_MonthDan monthdan)
        {
            using (CIMEntities en = new CIMEntities())
            {
                try
                {
                    en.AddObject("MonthDanSet", monthdan);
                    en.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    WebUtility.Print(ex.Message);
                    return false;
                }
            }
        }

        public bool EditMonthDan(Sys_MonthDan monthdan)
        {
            using (CIMEntities en = new CIMEntities())
            {
                try
                {
                    Sys_MonthDan Monthdan = en.MonthDanSet.Where("it.DanKey=@DanKey", new ObjectParameter("DanKey", monthdan.DanKey)).FirstOrDefault();
                    Monthdan.Year = monthdan.Year;
                    Monthdan.Month = monthdan.Month;
                    Monthdan.MonthDan = monthdan.MonthDan;
                    Monthdan.BasicNum = monthdan.BasicNum;
                    Monthdan.BasicMoney = monthdan.BasicMoney;
                    Monthdan.ExceedNum = monthdan.ExceedNum;
                    Monthdan.ExceedMoney = monthdan.ExceedMoney;
                    
                    en.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public void DelMonthDan(int DanKey)
        {
            CIMDataContext db = new CIMDataContext();
            int count = 0;

            if (count > 0)
            {
                WebUtility.JsAlertToBack("已在使用中,不能删除");
                HttpContext.Current.Response.End();
            }
            else
            {
                using (CIMEntities en = new CIMEntities())
                {
                    var Monthdan = en.MonthDanSet.FirstOrDefault(a => a.DanKey == DanKey);
                    en.DeleteObject(Monthdan);
                    en.SaveChanges();
                }
            }
        }

        public bool IsYearMonthExist(string Year, string Month)
        {
            using (CIMEntities db = new CIMEntities())
            {
                int count = db.MonthDanSet.Count(m => m.Year == Year && m.Month == Month);

                return count > 0 ? true : false;
            }
        }

        //-----------Sys_MonthDan End--------------------//
    }
}
